package com.admin.service.lend_book_service;


import com.admin.dao.adminlogindao.AdminDaoImpl;
import com.admin.dao.adminlogindao.AdminDaoInf;
import com.pojo.BorInfoBean;
import com.pojo.BorrowerBean;
import com.utils.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;

public class LendOperateImpl implements LendOperate {

    AdminDaoInf adminDao=new AdminDaoImpl();
    @Override
    public BorrowerBean queryUserIdByIdCard(String idCard) {
        BorrowerBean borrowerBean = new BorrowerBean();
        Connection conn = null;
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);
            String sql = "select userid from borrower where idcard=?";
            borrowerBean = adminDao.queryUserIdByIdCard(sql, conn,idCard);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return borrowerBean;
    }

    /**
     * 添加借阅信息
     * @param borInfoBean
     * @return
     */
    @Override
    public boolean addLendInfo(BorInfoBean borInfoBean) {
        boolean bool=false;
        Connection conn = null;
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);
            String sql = "insert into borinfo(borid,bookid,repaydate,price,state) values(?,?,?,?,?)";
            bool = adminDao.addLendInfo(sql, conn,borInfoBean);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return bool;
    }



    @Override
    public boolean reduceUserStockAndBalance(long userId, double borrowPrice) {
        boolean bool=false;
        Connection conn = null;
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);
            String sql = "update borrower set borrowingvolume=borrowingvolume+1,balance=balance-? where userid=? and balance!=0";
            bool = adminDao.reduceUserStockAndBalance(sql, conn,userId,borrowPrice);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return bool;
    }
}
